/*
 ============================================================================
 Name		: FEEditor.cpp
 Author	  : shangguan
 Version	 : 1.0
 Copyright   : this class for file open and save
 Description : CFEEditor implementation
 ============================================================================
 */
#include "FEEditor.h"
#include <f32file.h> 
#include <aknnotewrappers.h> 

CFEEditor::CFEEditor() :
	iFs(CEikonEnv::Static()->FsSession())
{

}

CFEEditor::~CFEEditor()
{

}

CFEEditor* CFEEditor::NewLC()
{
	CFEEditor* self = new (ELeave) CFEEditor();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CFEEditor* CFEEditor::NewL()
{
	CFEEditor* self = CFEEditor::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CFEEditor::ConstructL()
{

}

/*******************************************************************************
 * @Name:       CreateFile 
 * @Author:     shangguan
 * @Purpose:    create a txt file
 * @Returns:    void
 * @Parameters:	aFn--The file path and name
 * @Remarks:    -
 ******************************************************************************/
TBool CFEEditor::CreateFile(TFileName aFn)
{
	TInt ret = iFile.Create(iFs, aFn, EFileWrite | EFileStreamText);
	if (ret == KErrAlreadyExists)
	{
		_LIT(inforNote,"This file already exist!");
		CAknInformationNote* infor = new (ELeave) CAknInformationNote;
		infor->ExecuteLD(inforNote);

		iFile.Close();
		return EFalse;
	}
	else
	{
		iFile.Close();
		return ETrue;
	}
}

/*******************************************************************************
 * @Name:       OpenL 
 * @Author:     shangguan
 * @Purpose:    open a txt file
 * @Returns:    void
 * @Parameters:	aFn--The file path and name
 * @Parameters:	aDes--A buffer to read txt file
 * @Remarks:    -
 ******************************************************************************/
void CFEEditor::OpenL(TFileName aFn, TDes &aDes)
{
	User::LeaveIfError(iFile.Open(iFs, aFn, EFileRead));

	TInt offset = 0;
	User::LeaveIfError(iFile.Seek(ESeekStart, offset));

	//TPtr8 p((TUint8 *) aDes.Ptr(), aDes.MaxSize());
	//iFile.Read(p);
	//aDes.SetLength(aDes.Length() / 2);

	TBuf8<5000> txt;
	iFile.Read(txt);
	aDes.Copy(txt);

	iFile.Close();
}

/*******************************************************************************
 * @Name:       SaveL 
 * @Author:     shangguan
 * @Purpose:    save to a txt file
 * @Returns:    void
 * @Parameters:	aFn--The file path and name
 * @Parameters:	aDes--A buffer write to txt file
 * @Remarks:    -
 ******************************************************************************/
void CFEEditor::SaveL(TFileName aFn, const TDes &aDes)
{
	TInt offset = 0;
	TPtrC8 ptrtxt((TUint8 *) aDes.Ptr(), aDes.Size());

	CleanupClosePushL(iFile);
	User::LeaveIfError(iFile.Replace(iFs, aFn, EFileWrite | EFileStreamText));
	User::LeaveIfError(iFile.Seek(ESeekEnd, offset));
	User::LeaveIfError(iFile.Write(ptrtxt));
	CleanupStack::PopAndDestroy();

	iFile.Close();
}

/*******************************************************************************
 * @Name:       SaveAsL 
 * @Author:     shangguan
 * @Purpose:    save to another txt file.The file need to be created first
 * @Returns:    void
 * @Parameters:	aFn--The file path and name
 * @Parameters:	aDes--A buffer write to txt file
 * @Remarks:    -
 ******************************************************************************/
void CFEEditor::SaveAsL(TFileName aFn, const TDes &aDes)
{
	CreateFile(aFn);
	SaveL(aFn, aDes);
}

